package com.kaiwukj.android.ufamily.mvp.ui.page.order;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderEvaluateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderPayParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.OrderRefundParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.HouseKeeperResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderDetailResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.OrderResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.utils.upload.OnUploadListener;
import com.kaiwukj.android.ufamily.utils.upload.UploadBean;
import com.kaiwukj.android.ufamily.utils.upload.UploadUtils;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class OrderPresenter extends BasePresenter<OrderContract.Model, OrderContract.View> {

    public static final int REFUND = 1;
    public static final int CANCEL = 2;
    public static final int PAY = 3;
    public static final int DELETE = 4;
    public static final int CANCEL_REFUND = 5;
    public static final int EVALUATE = 6;

    @Inject
    public OrderPresenter(OrderContract.Model model, OrderContract.View view) {
        super(model, view);
    }

    public void fetchList(int status, int page, int pageSize) {
        mModel.fetchOrder(page, pageSize, status)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<OrderResult>>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<OrderResult> result) {
                        mRootView.onFetchOrder(result.getList());
                    }
                });
    }


    public void getOrderById(int id) {
        mModel.getOrderById(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<OrderDetailResult>() {
                    @Override
                    public void onSuccess(@NonNull OrderDetailResult result) {
                        mRootView.onGetOrder(result);
                    }
                });
    }

    public void payOrder(OrderPayParams payParams) {
        mModel.payOrder(payParams)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Map<String, Object>>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull Map<String, Object> result) {
                        mRootView.onOrderPayResult(result);
                    }
                });
    }

    public void evaluateOrder(OrderEvaluateParams params) {
        mModel.addEvaluate(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, EVALUATE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void submitEvaluate(OrderEvaluateParams params, List<String> imgList) {
        if (imgList == null || imgList.size() == 0) {
            evaluateOrder(params);
        } else {
            mModel.getQINiuToken()
                    .compose(RxUtils.schedulerHelper())
                    .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .subscribe(new ApiCallBack<String>(mRootView) {
                        @Override
                        public void onSuccess(@NonNull String result) {
                            UploadUtils.getInstance().uploadImages(imgList, result, new OnUploadListener() {
                                @Override
                                public void onUploadImage(UploadBean result) {

                                }

                                @Override
                                public void onUploadImages(List<UploadBean> results) {
                                    StringBuilder sb = new StringBuilder();
                                    boolean isFirst = true;
                                    for (UploadBean img : results) {
                                        if (!isFirst) {
                                            sb.append(",");
                                        }
                                        sb.append(img.getNormalUrl());
                                        isFirst = false;
                                    }
                                    params.setImg(sb.toString());
                                    evaluateOrder(params);
                                }

                                @Override
                                public void onError(int errCode, String errMsg) {
                                    mRootView.showMessage(errCode + ":" + errMsg);
                                    mRootView.onTaskErr(EVALUATE);
                                }
                            });
                        }
                    });
        }
    }

    public void cancelOrder(int orderId) {
        mModel.cancelOrder(orderId)
                .doOnSubscribe(act -> mRootView.showLoading("正在取消..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, CANCEL) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void deleteOrder(int orderId) {
        mModel.deleteOrder(orderId)
                .doOnSubscribe(act -> mRootView.showLoading("正在请求..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, DELETE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }


    public void refundOrder(OrderRefundParams params) {
        mModel.refundOrder(params)
                .doOnSubscribe(act -> mRootView.showLoading("正在提交"))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, REFUND) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.hideLoading();
                    }
                });

    }

    public void cancelRefund(int orderId) {
        mModel.cancelRefund(orderId)
                .doOnSubscribe(act -> mRootView.showLoading("正在请求..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, CANCEL_REFUND) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("操作成功");
                    }
                });
    }


    public void submitRefund(OrderRefundParams params, List<String> imgList) {
        if (imgList == null || imgList.size() == 0) {
            refundOrder(params);
            return;
        }
        mModel.getQINiuToken()
                .compose(RxUtils.schedulerHelper())
                .doOnSubscribe(ac -> mRootView.showLoading("正在处理..."))
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<String>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull String result) {
                        UploadUtils.getInstance().uploadImages(imgList, result, new OnUploadListener() {
                            @Override
                            public void onUploadImage(UploadBean result) {

                            }

                            @Override
                            public void onUploadImages(List<UploadBean> results) {
                                StringBuilder sb = new StringBuilder();
                                boolean isFirst = true;
                                for (UploadBean img : results) {
                                    if (!isFirst) {
                                        sb.append(",");
                                    }
                                    sb.append(img.getNormalUrl());
                                    isFirst = false;
                                }
                                params.setProof(sb.toString());
                                refundOrder(params);
                            }

                            @Override
                            public void onError(int errCode, String errMsg) {

                            }
                        });
                    }
                });

    }

    public void getKeeper() {
        mModel.getHouseKeeperResult()
                .doOnSubscribe(act -> mRootView.showLoading())
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<HouseKeeperResult>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull HouseKeeperResult result) {
                        mRootView.onGetHouseKeeper(result);
                    }
                });
    }

}
